package com.demo.recycleviewitemdecorationdemo;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

/**
 * 作者:zwk
 * 联系方式：497981239@qq.com
 * 时间：on 2019/7/11 20:06
 * 功能：通过绘制一个rect 做一个横线
 */
public class CustomItemDecoration extends RecyclerView.ItemDecoration {
	private Paint paint;
	private int devideHeight;

	public CustomItemDecoration() {
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setColor(Color.RED);
	}

	@Override
	public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
		int count = parent.getChildCount();
		for (int i = 0; i < count; i++) {
			View childView = parent.getChildAt(i);
			int index = parent.getChildAdapterPosition(childView);
			if (index != 0) {
				int devideTop = childView.getTop() - devideHeight;
//				int devideLeft = parent.getPaddingLeft(); // 线条的长度会和item的长度想同
				int devideLeft = parent.getPaddingLeft()+15; // 线条的长度会和item的长度想同
				Log.d("zwk", "————————————————————————————" + devideLeft);
				int bottom = childView.getTop();
				int devideRight = parent.getWidth() - parent.getPaddingRight();
				c.drawRect(devideLeft, devideTop, devideRight, bottom, paint);
			}
		}
	}

	@Override
	public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
		super.getItemOffsets(outRect, view, parent, state);
		if (parent.getChildAdapterPosition(view) != 0) {
			outRect.top = 1;
			devideHeight = 1;

		}
	}
}
